package cn.edu.gench.learn.dao;

import cn.edu.gench.learn.model.City;
import cn.edu.gench.learn.model.District;
import cn.edu.gench.learn.model.Province;
import cn.edu.gench.learn.util.ConnectFactory;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/5/5.
 */
@Service
public class CityDao {
    public List<Province> queryProvince() {

        String sql = "select * from d12_province";
        return null;
    }

    public List<City> queryCityByPId(int pid) {


        List<City> cityModels = new ArrayList<City>();
        try {
            // 加载驱动程序
            Connection conn = ConnectFactory.getConnection();
            Statement statement = conn.createStatement();
            // 要执行的SQL语句
            String sql = "select * from d12_city where pid = " + pid;
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                City city = new City();
                city.setName(rs.getString("name"));
                city.setId(rs.getInt("id"));
                city.setPid(rs.getInt("pid"));
                cityModels.add(city);
            }
            ConnectFactory.close(conn,rs,statement);
        } catch(SQLException e) {
            e.printStackTrace();
        }
        return cityModels;


    }

    public List<District> queryDistrictByCid(int cid) {
        String sql = "select * from d12_district where cid = " + cid;
        return null;

    }
}
